using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Clue_Less_Client.localhost;

namespace Clue_Less_Client
{
    public partial class Suggest : Form
    {
        private int _gameID;
        private Player _player;
        private MainForm _frm;
        private bool complete = false;
        private Rooms _room;
        private List<WeaponPosition> _weapons = new List<WeaponPosition>();
        private List<Character> _characters = new List<Character>();


        public Suggest(MainForm frm)
        {
            _gameID = frm.GameID;
            _player = frm.Player;
            _frm = frm;
            InitializeComponent();
        }

        private void Suggest_Load(object sender, EventArgs e)
        {
            btnSuggest.Text = "Suggest";

            _weapons = new List<WeaponPosition>(_frm.WS.GetWeapons(_gameID));
            _characters = new List<Character>(_frm.WS.GetCharactersPlaying(_gameID, "0"));

            _room = _frm.Board.GetCell(_frm.Player.XPosition, _frm.Player.YPosition).Room;
            panelImage.BackgroundImage = new Bitmap(Images.largeRooms[_room]);
            
            List<ComboBoxItem> weapons = new List<ComboBoxItem>();
            foreach (Weapons key in Images.weaponCards.Keys)
            {
                ComboBoxItem item = new ComboBoxItem();
                item.Value = key.ToString();
                item.Label = key.ToString();
                weapons.Add(item);
            }

            cbWeapons.DataSource = weapons;
            cbWeapons.DisplayMember = "Label";
            cbWeapons.ValueMember = "Value";

            List<ComboBoxItem> characters = new List<ComboBoxItem>();
            foreach (Characters key in Images.characterCards.Keys)
            {
                ComboBoxItem item = new ComboBoxItem();
                item.Value = key.ToString();
                item.Label = key.ToString();
                characters.Add(item);
            }

            cbCharacter.DataSource = characters;
            cbCharacter.DisplayMember = "Label";
            cbCharacter.ValueMember = "Value";

            drawCards();
        }

        private void drawCards()
        {
            // draw weapons
            int index = 1;
            for (int i = 0; i < _weapons.Count; i++)
            {
                BoardCell cell = _frm.Board.GetCell(_room);
                if (cell != null &&
                    _weapons[i].XPosition == cell.XPosition &&
                    _weapons[i].YPosition == cell.YPosition)
                {
                    Control ctrl = _frm.GetControlByName(this.panelImage, "card" + index);
                    if (ctrl != null)
                    {
                        PictureBox pb = (PictureBox)ctrl;
                        pb.Image = Images.weaponCards[_weapons[i].Weapon];
                        pb.Tag = _weapons[i].Weapon;
                        index++;
                    }
                }
            }
            // hide unused
            for (int i = 1; i <= 6; i++)
            {
                Control ctrl = _frm.GetControlByName(this.panelImage, "card" + i);
                if (ctrl != null && ctrl.Tag == null)
                    ctrl.Visible = false;
                else
                    ctrl.Visible = true;
            }

            // draw characters
            index = 1;
            for (int i = 0; i < _characters.Count; i++)
            {
                BoardCell cell = _frm.Board.GetCell(_room);
                if (cell != null &&
                    _characters[i].XPosition == cell.XPosition &&
                    _characters[i].YPosition == cell.YPosition)
                {
                    Control ctrl = _frm.GetControlByName(this.panelImage, "char" + index);
                    if (ctrl != null)
                    {
                        PictureBox pb = (PictureBox)ctrl;
                        pb.Image = Images.characterCards[_characters[i].CharacterType];
                        pb.Tag = _characters[i].CharacterType;
                        index++;
                    }
                }

            }
            // hide unused
            for (int i = 1; i <= 6; i++)
            {
                Control ctrl = _frm.GetControlByName(this.panelImage, "char" + i);
                if (ctrl != null && ctrl.Tag == null)
                    ctrl.Visible = false;
                else
                    ctrl.Visible = true;
            }
        }

        private void cbWeapons_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbWeapons.SelectedIndex >= 0)
            {
                pictureBox2.Image = Images.weaponCards[(Weapons)Enum.Parse(typeof(Weapons), ((ComboBoxItem)cbWeapons.SelectedItem).Value)];
            }
        }

        private void cbCharacter_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbCharacter.SelectedIndex >= 0)
            {
                pictureBox3.Image = Images.characterCards[(Characters)Enum.Parse(typeof(Characters), ((ComboBoxItem)cbCharacter.SelectedItem).Value)];
            }
        }

        private void btnSuggest_Click(object sender, EventArgs e)
        {
            if (cbWeapons.SelectedIndex >= 0 &&
                    cbCharacter.SelectedIndex >= 0)
                {
                    btnSuggest.Enabled = false;
                    if (_frm.WS.Suggest(_gameID, _player.PlayerID,
                        _room,
                        (Characters)Enum.Parse(typeof(Characters), ((ComboBoxItem)cbCharacter.SelectedItem).Value),
                        (Weapons)Enum.Parse(typeof(Weapons), ((ComboBoxItem)cbWeapons.SelectedItem).Value)))
                    {
                        MessageBox.Show("Congratulations! You have solved the mystery!", "Congratulations", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.Close();
                    }
                    else
                    {
                        BoardCell cell = _frm.Board.GetCell(_room);
                        foreach (WeaponPosition wp in _weapons)
                        {
                            if (wp.Weapon == (Weapons)Enum.Parse(typeof(Weapons), ((ComboBoxItem)cbWeapons.SelectedItem).Value)) 
                            {
                                wp.XPosition = cell.XPosition;
                                wp.YPosition = cell.YPosition;
                            }
                        }

                        foreach (Character c in _characters)
                        {
                            if (c.CharacterType == (Characters)Enum.Parse(typeof(Characters), ((ComboBoxItem)cbCharacter.SelectedItem).Value))
                            {
                                c.XPosition = cell.XPosition;
                                c.YPosition = cell.YPosition;
                            }
                        }

                        drawCards();
                        lblStatus.Text = "Waiting for other players to disprove.";
                        timerGame.Start();
                    }
                }
                else
                {
                    MessageBox.Show("Please make selection.", "Select cards", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }

        private void timerGame_Tick(object sender, EventArgs e)
        {
            _frm.WS.GetSuggestionAsync(_frm.GameID, Guid.NewGuid());
            _frm.WS.GetSuggestionCompleted += new GetSuggestionCompletedEventHandler(WS_GetSuggestionCompleted);
        }

        void WS_GetSuggestionCompleted(object sender, GetSuggestionCompletedEventArgs e)
        {
            if (e.Error == null) {
                Suggestion suggestion = e.Result;
                if (suggestion.IsDisproved)
                {
                    timerGame.Stop();
                    complete = true;
                    lblStatus.Text = suggestion.DisproverName + " has disproved your suggestion with " + suggestion.DisprovalCard.Name;
                }
            }
        }

        private void Suggest_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!complete)
            {
                e.Cancel = true;
                MessageBox.Show("Please be patient. First make suggestion and then let other players think about your suggestion." +
                    " Once the suggestion is disproved, you'll be able to close this window.", "Can't Close Yet.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
    }
}